package com.snj.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo3 {
    public static void main(String[] args) {
        // 初始化示例map，key为部门名称，value为部门人数
        Map<String, Integer> deptMap = new HashMap<>();
        deptMap.put("施工", 25);
        deptMap.put("服务", 5);

        // 初始化空的 mapList
        Map<String, List<Integer>> mapList = new HashMap<>();

        int totalFlag = 0;

        // 遍历 deptMap，动态处理每个部门
        for (Map.Entry<String, Integer> entry : deptMap.entrySet()) {
            String deptName = entry.getKey();
            Integer deptValue = entry.getValue();

            // 创建对应的List
            List<Integer> deptList = new ArrayList<>();

            // 计算整除结果
            int quotient = deptValue / 13;
            if (quotient > 0) {
                for (int i = 0; i < quotient; i++) {
                    deptList.add(13);
                }
            }

            int remainder = deptValue % 13;
            int flag = 0;

            boolean isValidDept = isValidRemainder(remainder);
            if (isValidDept) {
                deptList.add(remainder);
            } else {
                if (remainder > 5) {
                    int[] ints = findTwoOddNumbers(remainder);
                    for (int i : ints) {
                        deptList.add(i);
                    }
                    flag = (ints.length == 1) ? 1 : 0;
                } else {
                    flag = remainder;
                }
            }

            System.out.println(deptName + " 剩余人数: " + flag);
            mapList.put(deptName, deptList);

            totalFlag += flag;
        }

        if (totalFlag > 0) {
            for (Map.Entry<String, List<Integer>> entry : mapList.entrySet()) {
                String key = entry.getKey();
                List<Integer> list = entry.getValue();
                if (!list.isEmpty()) {
                    int index = 0;
                    for (Integer i : list) {
                        if (i < 13 && i + totalFlag < 13) {
                            boolean validJiShu = isValidJiShu(i + totalFlag);
                            if (validJiShu) {
                                list.set(index, i + totalFlag);
                            } else {
                                list.set(index, i + totalFlag + 1);
                            }
                            totalFlag = 0;
                            break;
                        } else if (i + totalFlag >= 13) {
                            list.set(index, 13);
                            totalFlag += i + totalFlag - 13;
                        }
                        index += 1;
                    }
                }
            }
        }

        System.out.println("总剩余: " + totalFlag);
        System.out.println(mapList);
    }

    private static boolean isValidRemainder(int remainder) {
        return remainder >= 5 && remainder <= 13 && remainder % 2 != 0;
    }

    private static boolean isValidJiShu(int value) {
        return value % 2 != 0;
    }

    private static int[] findTwoOddNumbers(int input) {
        int[] oddNumbers = {5, 7, 9, 11, 13};
        for (int i = 0; i < oddNumbers.length; i++) {
            for (int j = i; j < oddNumbers.length; j++) {
                if (oddNumbers[i] + oddNumbers[j] == input) {
                    return new int[]{oddNumbers[i], oddNumbers[j]};
                }
            }
        }
        return new int[]{input - 1};
    }
}